﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;

/// <summary>
/// Static class for logging purpose
/// </summary>
public static class Logging
{
    /// <summary>
    /// Write Log to a csv file.
    /// </summary>
    /// <param name="msg">Any message string to log</param>
    public static void WriteLog(string msg)
    {
        try
        {
            string lpath = ConfigurationManager.AppSettings["logPath"];
            string filename = Path.Combine(lpath, "Log-" + DateTime.Now.ToString("yyyyMMdd") + ".csv");
            if (ConfigurationManager.AppSettings["writeToLog"].Equals("1"))
            {
                using (StreamWriter sw = new StreamWriter(filename, true))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + msg.Replace("\t"," "));
                    sw.Close();
                }
            }
        }
        catch { }
    }

    /// <summary>
    /// Write log to table 
    /// </summary>
    /// <param name="logid">Log correlation ID</param>
    /// <param name="url">Url that cause an error</param>
    /// <param name="type">Exception type</param>
    /// <param name="message">Error message</param>
    /// <param name="stack">Error stack trace</param>
    /// <param name="creaby">Reporter</param>
    public static void WriteDB(string logid, string url, string type, string message, string stack, string creaby)
    {
        try
        {
            DateTime sys = DateTime.Now;
            /* Write to Log Table */
            using (DC_AHMIDEADataContext dcl = new DC_AHMIDEADataContext())
            {
                AHMIDEA_MSTLOG tlog = new AHMIDEA_MSTLOG();
                tlog.vlogid = logid;
                tlog.vurl = url;
                tlog.vtype = type;
                tlog.vmessage = message;
                tlog.vstack = stack;
                tlog.dcrea = sys;
                tlog.vcrea = creaby;

                dcl.AHMIDEA_MSTLOGs.InsertOnSubmit(tlog);
                dcl.SubmitChanges();
            }

            /* Write to Log file */
            WriteLog("SUBMITTED_BY: " + creaby + "; URL : " + url + "; ERRORTYPE: " + type + "; MESSAGE: " + message + "; STACK: " + stack);
        }
        catch { }
    }

    /// <summary>
    /// Error Confirmation from user. This method called when a user clicks the report error button. 
    /// </summary>
    /// <param name="logid">Log correlation ID</param>
    /// <param name="getData">GET data passed to server</param>
    /// <param name="postData">POST data passed to server</param>
    /// <param name="confirmedBy">Error Reporter</param>
    public static void ConfirmError(string logid, string getData, string postData, string confirmedBy) {
        try
        {
            DateTime sys = DateTime.Now;
            using (DC_AHMIDEADataContext dcl = new DC_AHMIDEADataContext())
            {
                AHMIDEA_MSTLOG tlog = new AHMIDEA_MSTLOG();
                var tlogitem = (from t in dcl.AHMIDEA_MSTLOGs where t.vlogid == logid select t).Single();
                tlogitem.vconfirm = confirmedBy;
                tlogitem.dconfirm = sys;
                tlogitem.vget = getData;
                tlogitem.vpost = postData;
                dcl.SubmitChanges();
            }            
        }
        catch { }
    }

}